package com.aididi.bean;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Auther: zx.xu
 * @Description:   KeyResolver需要实现resolve方法，比如根据Hostname进行限流，则需要用hostAddress去判断。
 *      实现完KeyResolver之后， 需要将这个类的Bean注册到Ioc容器中
 */
public class HostAddrKeyResolver implements KeyResolver {


    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        //IP限流
        return Mono.just(
                exchange.getRequest()
                .getRemoteAddress()
                .getAddress()
                .getHostAddress());
        //根据uri去限流
       // return Mono.just(exchange.getRequest().getURI().getPath());
        //以用户的维度去限流
       // Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
    }
}
